﻿/*  Copyright (C) 2012  Mark Hamburger
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */


using System;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace removeav2012
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            refreshFilelist(); //populate the list on run.
        }

        private void killButton_Click(object sender, EventArgs e)
        {
            if (fileListbox.SelectedIndex >= 0) //check to make sure there is something selected (prevents crashes)
            {
                string badfile = fileListbox.SelectedItem.ToString(); //takes the selected file name and gets rid of the path and extensions

                getandkill(badfile); //kill the process
            }
            
            else return;

        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            refreshFilelist(); //just refresh the file list.
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (exeFileListbox.SelectedIndex >= 0) //make sure something is selected.
            {
                string badfile = exeFileListbox.SelectedItem.ToString();

                if (System.IO.File.Exists(badfile)) //check to make sure file exists
                {
                    // Use a try block to catch IOExceptions, to
                    // handle the case of the file already being
                    // opened by another process.
                    try
                    {
                        getandkill(badfile); //kill it first. helps to make sure file can be deleted
                        System.IO.File.Delete(badfile); //delete the bad file.
                        refreshFilelist(); //refresh the list
                        MessageBox.Show("Deleted " + badfile);
                    }
                    catch (System.IO.IOException errorout) // if file cant be deleted, display error
                    {
                        label1.Text = errorout.ToString();
                        return;
                    }
                }
            }
            

            if (fileListbox.SelectedIndex >= 0) //make sure something is selected.
            {
                string badfile = fileListbox.SelectedItem.ToString();

                if (System.IO.File.Exists(badfile)) //check to make sure file exists
                {
                    // Use a try block to catch IOExceptions, to
                    // handle the case of the file already being
                    // opened by another process.
                    try
                    {
                        // getandkill(badfile); //kill it first. helps to make sure file can be deleted (not necessery for files in this box)
                        System.IO.File.Delete(badfile); //delete the bad file.
                        refreshFilelist(); //refresh the list
                        MessageBox.Show("Deleted " + badfile);
                    }
                    catch (System.IO.IOException errorout) // if file cant be deleted, display error
                    {
                        label1.Text = errorout.ToString();
                        return;
                    }
                }
            }
            
        }


        private void fixexeButton_Click(object sender, EventArgs e)
        {
            string ncrReg = Application.StartupPath + "\\FixNCR.reg";         //path to FixNCR.Reg

            if (File.Exists(ncrReg))
            {
                Process regeditProcess = Process.Start("regedit.exe", " \"" + ncrReg +"\""); //insert registry fix for exe's
                regeditProcess.WaitForExit();
            }
            else
            {
                downloadForm newform = new downloadForm();
                newform.Show();
            }

        }

        private void refreshFilelist()
        {
            fileListbox.Items.Clear();                  //clear the box
            exeFileListbox.Items.Clear();
            
            string localappdatapath = Environment.GetEnvironmentVariable("LOCALAPPDATA"); //get the relevent environment variables.
            string alluserprofile = Environment.GetEnvironmentVariable("ALLUSERSPROFILE");
            string appdata = Environment.GetEnvironmentVariable("APPDATA");
            
            string[] ladexefilenames = Directory.GetFiles(localappdatapath, "???.exe"); //get a list all of the 3.exe files
            string[] ladfilenames = Directory.GetFiles(localappdatapath, "*.");
            string[] aupfilenames = Directory.GetFiles(alluserprofile, "*.");
            string[] adfilenames = Directory.GetFiles(appdata, "*.");

            exeFileListbox.Items.AddRange(ladexefilenames);
            fileListbox.Items.AddRange(ladfilenames);      //display the filenames in the list box
            fileListbox.Items.AddRange(aupfilenames);
            fileListbox.Items.AddRange(adfilenames);

        }

        private void getandkill(string processname)     //class to kill processes
        {
            string  bProc = Path.GetFileNameWithoutExtension(processname); //strip the path and extension
            Process[] processes = Process.GetProcessesByName(bProc); //find all processes by that name

            if (processes.Length > 0)
            {
                foreach (Process process in processes)      //go thru the array and kill each process
                {
                    process.Kill();
                    label1.Text = "Killed " + bProc;      //indicate to the user that something was done.
                }
            }
            else
            {
                label1.Text = "could not find process " + bProc;
            }
        }
        
    

        
        private void tempButton_Click(object sender, EventArgs e)
        {
            Process inetTemp = Process.Start("rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 8");  // Delete temps
            inetTemp.WaitForExit();
            
            StreamWriter sw = new StreamWriter("deltemp.bat"); // This section creates a batch file to delete the temp files and folders recursively
            sw.WriteLine("del /q /s /f %temp%");
            sw.Close();

            Process delTemp = Process.Start("deltemp.bat"); //run the batch file to delete %TEMP%
            delTemp.WaitForExit();  //wait for it to finish

            File.Delete("deltemp.bat"); //delete the batch file
            MessageBox.Show("Completed Successfully");

        }

        private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            helpForm newHelpForm = new helpForm();
            newHelpForm.Show();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            {
                AboutBox1 myNewForm = new AboutBox1();
                myNewForm.ShowDialog();
            }
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            fileListbox.ClearSelected();
            exeFileListbox.ClearSelected();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            modeSelect modeselect = new modeSelect();
            modeselect.Show();
            
        }
    }
}
